const  tools = require("./tools");
const  db = require("./db");
const  {upPic} = require("./upPic");
const mongodb = require("mongodb")
//添加商品类别
module.exports.addGoodsTypeList=async function(req,res){
    const {_id,shopName,shopTypeId,shopTypeName} = await db.findOneById("shopList",req.body.shopId);

    await db.insertOne("goodsTypeList",{
        goodsTypeName:req.body.goodsTypeName,
        shopId:_id,
        shopName,
        shopTypeId,
        shopTypeName,
        createTime:Date.now()
    })
    res.json({
        ok:1,
        msg:"插入成功"
    })
}
//获取商品类别
module.exports.getGoodsTypeList=async function(req,res){
    let pageIndex = req.query.pageIndex/1;
    let pageSum = 1;
    let limit = 2;
    let whereObj = {};
    // if(req.query.shopTypeId.length>0)
    //     whereObj.shopTypeId = mongodb.ObjectId(req.query.shopTypeId);
    const count = await db.count("goodsTypeList",whereObj);
    pageSum = Math.ceil(count/limit);
    if(pageSum < 1)
        pageSum = 1;
    if(pageIndex > pageSum)
        pageIndex = pageSum;
    if(pageIndex < 1)
        pageIndex = 1;
    const goodsTypeList = await db.find("goodsTypeList",{
        whereObj,
        sortObj:{
            createTime:-1
        },
        skip:(pageIndex-1)*limit,
        limit
    });
    // console.log(goodsTypeList)
    res.json({
        ok:1,
        goodsTypeList,
        pageIndex,
        pageSum
    })
}
module.exports.getGoodsListByTypeId=async function(req,res){
    const shopId = mongodb.ObjectId(req.params.shopId)
    // console.log(shopId)
    const goodsTypeList = await db.find("goodsTypeList",{
        whereObj:{
            shopId
        },
        sortObj:{
            createTime:-1
        }
    })
    res.json({
        ok:1,
        goodsTypeList
    })
}